---
- name: ensure packages required for imagefactory are installed (dnf)
  dnf: name={{ item }} state=present enablerepo={{ extra_enablerepos }}
  with_items:
    - python-libguestfs
    - libguestfs-tools
    - qemu-img
    - git

- name: create directory for git clone
  file: path=/var/lib/fedoraqa state=directory owner=root group=root mode=1755

- name: create directory for configuration
  file: path=/etc/taskotron_base_images state=directory owner=root group=root mode=0755

- name: create directory for disposable clients images
  file: path={{ imagesdir }} state=directory owner=root group={{ slaves_group }} mode=1755

- name: Check out our imagefactory tools
  git:
    repo: https://pagure.io/taskotron/base_images.git
    dest: '/var/lib/fedoraqa/base_images'
    update: yes
    version: 'master'
  register: git_result
  changed_when: "git_result.after|default('after') != git_result.before|default('before')"

- name: Generate configuration
  template: src={{ item }} dest=/etc/taskotron_base_images/config.ini owner=root group=root mode=0644
  with_first_found:
    - config.ini.j2.{{ deployment_type }}
    - config.ini.j2

- name: Download/update images
  command: '/var/lib/fedoraqa/base_images/process_for_taskotron.py --taskotrondir {{imagesdir}}'

- name: Create cronjob to download new images
  cron:
    name: "Download new images from ImageFactory"
    special_time: hourly
    job: '/var/lib/fedoraqa/base_images/process_for_taskotron.py --taskotrondir {{imagesdir}} > /dev/null'
- name: Create cronjob to prune old images
  cron:
    name: "Clean up images downloaded from ImageFactory"
    special_time: daily
    job: '/var/lib/fedoraqa/base_images/prune_images/prune_images.py directory --keep 3 {{imagesdir}} > /dev/null'
